package com.turing.post.organization.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.ConsumeTerminal;
import com.turing.post.organization.util.ConsumeTerminalDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSWebInput;

/**
 * 商户脱机消费终端管理Dao实现
 * @author kangzhe
 *
 */
@Stateless(mappedName = "organization.impl.ConsumeTerminalDaoImpl")
@Remote
@Local
public class ConsumeTerminalDaoImpl implements ConsumeTerminalDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;
	
	@SuppressWarnings("unchecked")
	@Override
	public ConsumeTerminal getConsumeTerminalByTerminalNo(String terminalNo) {
		String sql = "select * from consumeterminal t where t.terminalno=?1";
		Query query = _em.createNativeQuery(sql, ConsumeTerminal.class);
		query.setParameter(1, terminalNo);
		List<ConsumeTerminal> ctList = query.getResultList();
		if (ctList != null && ctList.size() > 0) {
			return ctList.get(0);
		}
		return null;
	}

	@Override
	public void insert(ConsumeTerminal ct) throws PCPSApplicationException {
		GeneralDao.insert(_em, ct);
	}

	@Override
	public void update(ConsumeTerminal ct) throws PCPSApplicationException {
		GeneralDao.update(_em, ct);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ConsumeTerminal> getConsumeTerminalListByConditons(
			PCPSWebInput webInput, String merchantNo, String merchantBranchNo,
			String terminalNo, Integer page) {
		if (webInput == null || webInput.getOperId() == null) {
			return null;
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from consumeterminal t " + "where 1=1 "
				+ "and (t.merchantno=?1 or ?1 is null) "
				+ "and (t.merchantbranchno=?2 or ?2 is null) "
				+ "and (t.terminalno=?3 or ?3 is null) "
				+ "order by t.merchantno,t.terminalno";
		Query query = _em.createNativeQuery(sql, ConsumeTerminal.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, merchantNo);
		query.setParameter(2, merchantBranchNo);
		query.setParameter(3, terminalNo);
		return query.getResultList();
	}

	@Override
	public Integer getConsumeTerminalTotalByConditons(PCPSWebInput webInput,
			String merchantNo, String merchantBranchNo, String terminalNo) {
		if (webInput == null || webInput.getOperId() == null) {
			return null;
		}
		String sql = "select nvl(count(*),0) from consumeterminal t "
				+ "where 1=1 " + "and (t.merchantno=?1 or ?1 is null) "
				+ "and (t.merchantbranchno=?2 or ?2 is null) "
				+ "and (t.terminalno=?3 or ?3 is null) ";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, merchantNo);
		query.setParameter(2, merchantBranchNo);
		query.setParameter(3, terminalNo);
		return new Integer(query.getSingleResult().toString());
	}

}
